package com.safat.adf.util;

import javax.el.ELContext;

import javax.el.ExpressionFactory;

import javax.el.MethodExpression;

import javax.faces.context.FacesContext;


public class ADFUtil {

    public static void onDeleteItem() {
        ADFUtil.invokeEL("#{bindings.Delete.execute}");
        ADFUtil.invokeEL("#{bindings.Commit.execute}");
    }

    public static void onCreateItem() {
        ADFUtil.invokeEL("#{bindings.Create.execute}");
        ADFUtil.invokeEL("#{bindings.CreateInsert.execute}");
    }

    public static void onCreateInsertItem() {
        ADFUtil.invokeEL("#{bindings.CreateInsert.execute}");
    }
    
    public static void onCommitOperation() {
        ADFUtil.invokeEL("#{bindings.Commit.execute}");
    }

    public static void onRollbackItem() {
        ADFUtil.invokeEL("#{bindings.Rollback.execute}");
    }

    public static Object invokeEL(String el) {
        return invokeEL(el, new Class[0], new Object[0]);
    }

    public static Object invokeEL(String el, Class[] paramTypes,
                                  Object[] params) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =
            facesContext.getApplication().getExpressionFactory();
        MethodExpression exp =
            expressionFactory.createMethodExpression(elContext, el,
                                                     Object.class, paramTypes);
        return exp.invoke(elContext, params);
    }
}
